﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;

using Ivy.Common.Framework.Base;
using Ivy.Common.Framework.DataAccess;
using Ivy.SiteAdmin.Entity;

namespace Ivy.SiteAdmin.DataAccess
{
    public class Sys_ErrorLogDao : IDaoInterface<Sys_ErrorLog, Sys_ErrorLogCriteria>
    {
        #region  Variable & constant
        #endregion  Variable & constant

        #region Constructor & Destructor
        #endregion Constructor & Destructor

        #region Properties
        #endregion Properties

        #region Public Methods
        public bool Insert(Sys_ErrorLog Sys_ErrorLogEntity)
        {
            bool IsOperationSuccessful = false;
            if (Sys_ErrorLogEntity.EntityState == EntityState.New)
            {
                DbConnection DbConn = DataAccessProviderUtility.CreateConnectionWithConnectionString();
                DbCommand CmdObj = DataAccessProviderUtility.CreateCommand();
                CmdObj.CommandText = "Dasideen_Sys_ErrorLog_Insert";
                CmdObj.CommandType = CommandType.StoredProcedure;

                TranslateSys_ErrorLogFromEntityToDatabaseParameters(CmdObj, Sys_ErrorLogEntity);

                DatabaseCommon.InsertStoredProcForParameterDatEntityBase(CmdObj, Sys_ErrorLogEntity);

                DataAccessProviderUtility.AddParameter(CmdObj, "@ErrorLogPrimaryID", ParameterDirection.Output, DbType.Int32, DBNull.Value);
                DataAccessProviderUtility.AddParameter(CmdObj, "@Sys_DateOfCreation", ParameterDirection.Output, DbType.DateTime, DBNull.Value);



                CmdObj.Connection = DbConn;
                DbConn.Open();
                CmdObj.Prepare();
                int _NoOfRecord = CmdObj.ExecuteNonQuery();
                if (_NoOfRecord == 1)
                {
                    Sys_ErrorLogEntity.ErrorLogPrimaryID = Convert.ToInt32(CmdObj.Parameters["@ErrorLogPrimaryID"].Value);
                    Sys_ErrorLogEntity.Sys_DateOfCreation = Convert.ToDateTime(CmdObj.Parameters["@Sys_DateOfCreation"].Value);
                    Sys_ErrorLogEntity.Sys_DateOfModification = Sys_ErrorLogEntity.Sys_DateOfCreation;
                    Sys_ErrorLogEntity.EntityState = EntityState.UnChanged;
                    IsOperationSuccessful = true;
                }
                CmdObj.Dispose();
                DbConn.Close();
            }
            else
            {

                throw new Exception("For Inserting new Sys_ErrorLog entity data to database,[EntityState] attribute should be [New]");
            }

            return IsOperationSuccessful;
        }

        public bool Update(Sys_ErrorLog Sys_ErrorLogEntity)
        {
            bool IsOperationSuccessful = false;

            if (Sys_ErrorLogEntity.EntityState == EntityState.Changed)
            {
                DbConnection DbConn = DataAccessProviderUtility.CreateConnectionWithConnectionString();
                DbCommand CmdObj = DataAccessProviderUtility.CreateCommand();
                CmdObj.CommandText = "Dasideen_Sys_ErrorLog_Update";
                CmdObj.CommandType = CommandType.StoredProcedure;

                DataAccessProviderUtility.AddParameter(CmdObj, "@ErrorLogPrimaryID", ParameterDirection.Input, DbType.Int32, Sys_ErrorLogEntity.ErrorLogPrimaryID);

                TranslateSys_ErrorLogFromEntityToDatabaseParameters(CmdObj, Sys_ErrorLogEntity);
                DatabaseCommon.UpdateStoredProcForParameterDatEntityBase(CmdObj, Sys_ErrorLogEntity);

                DataAccessProviderUtility.AddParameter(CmdObj, "@Sys_DateOfModification", ParameterDirection.Output, DbType.DateTime, DBNull.Value);

                CmdObj.Connection = DbConn;
                DbConn.Open();
                CmdObj.Prepare();
                int _NoOfRecords = CmdObj.ExecuteNonQuery();
                if (_NoOfRecords == 1)
                {
                    Sys_ErrorLogEntity.Sys_DateOfModification = Convert.ToDateTime(CmdObj.Parameters["@Sys_DateOfModification"].Value);
                    Sys_ErrorLogEntity.EntityState = EntityState.UnChanged;
                    IsOperationSuccessful = true;
                }

                CmdObj.Dispose();
                DbConn.Close();
            }
            else
            {
                throw new Exception("For Updating existing Sys_ErrorLog entity data to database, [EntityState] attribute should be [Changed]");
            }

            return IsOperationSuccessful;
        }

        public bool Delete(Sys_ErrorLog DataEntity)
        {
            throw new NotImplementedException();
        }

        public Sys_ErrorLog GetByID(string ID)
        {
            throw new NotImplementedException();
        }

        public List<Sys_ErrorLog> GetAll()
        {
            List<Sys_ErrorLog> lSys_ErrorLogList = new List<Sys_ErrorLog>();

            DbConnection DbConn = DataAccessProviderUtility.CreateConnectionWithConnectionString();
            DbCommand CmdObj = DataAccessProviderUtility.CreateCommand();
            CmdObj.CommandText = "Dasideen_Sys_ErrorLog_GetAll";
            CmdObj.CommandType = CommandType.StoredProcedure;

            CmdObj.Connection = DbConn;
            DbConn.Open();
            CmdObj.Prepare();

            DbDataReader _DataReader = CmdObj.ExecuteReader();
            if (_DataReader.HasRows)
            {
                while (_DataReader.Read())
                {
                    Sys_ErrorLog _TempSys_ErrorLog = new Sys_ErrorLog(); //IMPORTANT NOTE:- Create instance only when required.
                    this.TranslateSys_ErrorLogFieldsFromReaderToEntity(_TempSys_ErrorLog, _DataReader);
                    lSys_ErrorLogList.Add(_TempSys_ErrorLog);
                }
            }
            CmdObj.Dispose();
            DbConn.Close();


            return lSys_ErrorLogList;
        }

        public List<Sys_ErrorLog> GetByCriteria(Sys_ErrorLogCriteria CriteriaEntity)
        {
            throw new NotImplementedException();
        }

        public Sys_ErrorLog GetByErrorLogPrimaryKey(int ErrorLogPrimaryID)
        {
            Sys_ErrorLog _SelectedSys_ErrorLog = null; //IMPORTANT NOTE:- Just declare the variable never create an instance, create instance only when required.

            DbConnection DbConn = DataAccessProviderUtility.CreateConnectionWithConnectionString();
            DbCommand CmdObj = DataAccessProviderUtility.CreateCommand();
            CmdObj.CommandText = "Dasideen_Sys_ErrorLog_GetByKey";
            CmdObj.CommandType = CommandType.StoredProcedure;

            DataAccessProviderUtility.AddParameter(CmdObj, "@ErrorLogPrimaryID", ParameterDirection.Input, DbType.Int32, Convert.ToInt32(ErrorLogPrimaryID));


            CmdObj.Connection = DbConn;
            DbConn.Open();
            CmdObj.Prepare();
            DbDataReader _DataReader = CmdObj.ExecuteReader();

            if (_DataReader.HasRows)
            {
                if (_DataReader.Read())
                {
                    _SelectedSys_ErrorLog = new Sys_ErrorLog(); //IMPORTANT NOTE:- Create instance only when required.
                    TranslateSys_ErrorLogFieldsFromReaderToEntity(_SelectedSys_ErrorLog, _DataReader);
                }
            }
            else
            {
                throw new Exception("No Sys_ErrorLog found");
            }
            CmdObj.Dispose();
            DbConn.Close();

            return _SelectedSys_ErrorLog;
        }

        #endregion Public Methods

        #region Events
        #endregion Events

        #region Private Methods
        private void TranslateSys_ErrorLogFieldsFromReaderToEntity(Sys_ErrorLog pSys_ErrorLog, DbDataReader pDbDataReader)
        {

            pSys_ErrorLog.ErrorLogPrimaryID = Convert.ToInt32(pDbDataReader["ErrorLogPrimaryID"]);
            pSys_ErrorLog.ApplicationName = Convert.ToString(pDbDataReader["ApplicationName"]);
            pSys_ErrorLog.MethodName = Convert.ToString(pDbDataReader["MethodName"]);
            pSys_ErrorLog.ErrorMessage = Convert.ToString(pDbDataReader["ErrorMessage"]);
            pSys_ErrorLog.StackTrace = Convert.ToString(pDbDataReader["StackTrace"]);


            DatabaseCommon.TranslateSystemFieldsFromReaderToEntity(pSys_ErrorLog, pDbDataReader);

        }
        private void TranslateSys_ErrorLogFromEntityToDatabaseParameters(DbCommand pDbCommand, Sys_ErrorLog pSys_ErrorLogEntity)
        {
            DataAccessProviderUtility.AddParameter(pDbCommand, "@ApplicationName", ParameterDirection.Input, DbType.String, pSys_ErrorLogEntity.ApplicationName);
            DataAccessProviderUtility.AddParameter(pDbCommand, "@MethodName", ParameterDirection.Input, DbType.String, pSys_ErrorLogEntity.MethodName);
            DataAccessProviderUtility.AddParameter(pDbCommand, "@ErrorMessage", ParameterDirection.Input, DbType.String, pSys_ErrorLogEntity.ErrorMessage);
            DataAccessProviderUtility.AddParameter(pDbCommand, "@StackTrace", ParameterDirection.Input, DbType.String, pSys_ErrorLogEntity.StackTrace);
        }
        #endregion Private Methods
        
    }
}
